package com.bckj.fastboot.core.extra.txasync.entity;

import org.springframework.util.Assert;

public class AsyncInvocationBuilder {
    /**
     * 任务ID
     */
    private String taskId;
    /**
     * 任务名称
     */
    private String taskName;
    /**
     * 方法名
     */
    private String methodName;
    /**
     * 类名
     */
    private String className;
    /**
     * 参数
     */
    private String params;
    /**
     * 状态 0 准备 1 运行中 2 异常 3 成功 4 失败
     */
    private Integer status = 0;
    /**
     * 初始化间隔时间 默认5s
     */
    private Long initialInterval = 5000L;
    /**
     * 乘数
     */
    private Double multiplier = 0D;
    /**
     * 超时时间 默认30s
     */
    private Long timeout = 30000L;
    /**
     * 重试次数
     */
    private Integer retryCount = 0;
    /**
     * 最大重试次数
     */
    private Integer maxAttempt = 5;

    public AsyncInvocationBuilder taskId(String taskId) {
        this.taskId = taskId;
        return this;
    }

    public AsyncInvocationBuilder taskName(String taskName) {
        this.taskName = taskName;
        return this;
    }

    public AsyncInvocationBuilder methodName(String methodName) {
        this.methodName = methodName;
        return this;
    }

    public AsyncInvocationBuilder className(String className) {
        this.className = className;
        return this;
    }

    public AsyncInvocationBuilder params(String params) {
        this.params = params;
        return this;
    }

    public AsyncInvocationBuilder status(Integer status) {
        this.status = status;
        return this;
    }

    public AsyncInvocationBuilder initialInterval(Long initialInterval) {
        this.initialInterval = initialInterval;
        return this;
    }

    public AsyncInvocationBuilder multiplier(Double multiplier) {
        this.multiplier = multiplier;
        return this;
    }

    public AsyncInvocationBuilder timeout(Long timeout) {
        if (timeout != null && timeout > 0) {
            this.timeout = timeout;
        }
        return this;
    }

    public AsyncInvocationBuilder retryCount(Integer retryCount) {
        this.retryCount = retryCount;
        return this;
    }

    public AsyncInvocationBuilder maxAttempt(Integer maxAttempt) {
        this.maxAttempt = maxAttempt;
        return this;
    }

    public AsyncInvocation build() {
        Assert.notNull(taskId, "taskId can not be blank");
        Assert.isTrue(initialInterval < timeout, "initialInterval must be less than timeout");
        AsyncInvocation asyncInvocation = new AsyncInvocation();
        asyncInvocation.setTaskId(taskId);
        asyncInvocation.setTaskName(taskName);
        asyncInvocation.setMethodName(methodName);
        asyncInvocation.setClassName(className);
        asyncInvocation.setParams(params);
        asyncInvocation.setStatus(status);
        asyncInvocation.setInitialInterval(initialInterval);
        asyncInvocation.setMultiplier(multiplier);
        asyncInvocation.setTimeout(timeout);
        asyncInvocation.setRetryCount(retryCount);
        asyncInvocation.setMaxAttempt(maxAttempt);
        return asyncInvocation;
    }
}
